package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.Passage;
import com.mycompany.myapp.service.PassageService;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import com.mycompany.myapp.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Passage.
 */
@RestController
@RequestMapping("/api")
public class PassageResource {

    private final Logger log = LoggerFactory.getLogger(PassageResource.class);

    private static final String ENTITY_NAME = "passage";

    private final PassageService passageService;

    public PassageResource(PassageService passageService) {
        this.passageService = passageService;
    }

    /**
     * POST  /passages : Create a new passage.
     *
     * @param passage the passage to create
     * @return the ResponseEntity with status 201 (Created) and with body the new passage, or with status 400 (Bad Request) if the passage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/passages")
    @Timed
    public ResponseEntity<Passage> createPassage(@RequestBody Passage passage) throws URISyntaxException {
        log.debug("REST request to save Passage : {}", passage);
        if (passage.getId() != null) {
            throw new BadRequestAlertException("A new passage cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Passage result = passageService.save(passage);
        return ResponseEntity.created(new URI("/api/passages/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /passages : Updates an existing passage.
     *
     * @param passage the passage to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated passage,
     * or with status 400 (Bad Request) if the passage is not valid,
     * or with status 500 (Internal Server Error) if the passage couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/passages")
    @Timed
    public ResponseEntity<Passage> updatePassage(@RequestBody Passage passage) throws URISyntaxException {
        log.debug("REST request to update Passage : {}", passage);
        if (passage.getId() == null) {
            return createPassage(passage);
        }
        Passage result = passageService.save(passage);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, passage.getId().toString()))
            .body(result);
    }

    /**
     * GET  /passages : get all the passages.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of passages in body
     */
    @GetMapping("/passages")
    @Timed
    public ResponseEntity<List<Passage>> getAllPassages(Pageable pageable) {
        log.debug("REST request to get a page of Passages");
        Page<Passage> page = passageService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/passages");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /passages/:id : get the "id" passage.
     *
     * @param id the id of the passage to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the passage, or with status 404 (Not Found)
     */
    @GetMapping("/passages/{id}")
    @Timed
    public ResponseEntity<Passage> getPassage(@PathVariable Long id) {
        log.debug("REST request to get Passage : {}", id);
        Passage passage = passageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(passage));
    }

    /**
     * DELETE  /passages/:id : delete the "id" passage.
     *
     * @param id the id of the passage to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/passages/{id}")
    @Timed
    public ResponseEntity<Void> deletePassage(@PathVariable Long id) {
        log.debug("REST request to delete Passage : {}", id);
        passageService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
